package com.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * 数据库帮助类
 * @author acer
 *
 */
public class DbHelper {

	public static Connection getConnection() {

		try {
//			Class.forName("com.mysql.jdbc.Driver");
//			return DriverManager.getConnection("jdbc:mysql://localhost:3306/news_db", "root", "root");

	         Context ctx = new InitialContext();
	         DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/news_db");
	         return ds.getConnection();	          
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	public static void close(Connection conn, PreparedStatement prep, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			if (prep != null)
				prep.close();

			if (conn != null)
				conn.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
